import sys
from threading import Thread
from typing import re

from PyQt5.QtCore import QModelIndex, QUrl, Qt, QPoint
from PyQt5.QtGui import QCursor
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout, QTableWidget, QTableWidgetItem, \
    QAbstractItemView, QMenu

from spilder import Spider


class MusicList(QWidget):
    def __init__(self):
        super().__init__()
        self.InitUI()
        self.loadList()
        self.mList: []
        self.selectIndex: int

    def InitUI(self):
        # 设置窗口的位置和大小
        self.setGeometry(300, 300, 300, 220)
        # 设置窗口的标题
        self.setWindowTitle('Icon')
        # 设置窗口的图标，引用当前目录下的web.png图片
        # self.setWindowIcon(QIcon('web.png'))

        ly = QVBoxLayout()
        self.mListTable = QTableWidget()
        self.mListTable.setEditTriggers(QAbstractItemView.NoEditTriggers)
        ly.addWidget(self.mListTable)

        self.contextMenu = QMenu(self)
        self.actionDown = self.contextMenu.addAction('down')
        self.actionDown.triggered.connect(self.downLoadAct)

        self.mListTable.doubleClicked.connect(self.doubleList)
        self.mListTable.setContextMenuPolicy(Qt.CustomContextMenu)
        self.mListTable.customContextMenuRequested.connect(self.showContextMenu)
        self.setLayout(ly)

        # 显示窗口
        self.show()

    def loadList(self):
        self.mListTable.setHorizontalHeaderLabels(['title', 'upper'])
        self.mListTable.setColumnCount(2)
        self.spider = Spider()
        mList = self.spider.getList()
        self.mList = mList
        self.mListTable.setRowCount(len(mList))

        for (i, v) in enumerate(mList):
            self.mListTable.setItem(i, 0, QTableWidgetItem(v['title']))
            self.mListTable.setItem(i, 1, QTableWidgetItem(v['upper']['name']))

    def doubleList(self, v: QModelIndex):
        try:
            self.player = QMediaPlayer(self)
            print(self.player.state())
            media_content = QMediaContent(
                QUrl.fromLocalFile('G:\\sourcesakura\\code\\bili-music\\view\\wxsn.mp3'))
            self.player.setMedia(media_content)
            self.player.setVolume(100)
            self.player.play()
            print(self.player.state())
            # self.player.
            print(self.player.media().request().url())
            # self.player.
        except Exception as e:
            print(e)
        # try:
        #     bv = self.mList[v.row()]['bv_id']
        #     audioUrl = self.spider.audioUrl('https://www.bilibili.com/video/' + bv)
        #     print(audioUrl)
        # except Exception as e:
        #     print(e)

    def showContextMenu(self, v: QPoint):
        self.selectIndex = self.mListTable.rowAt(v.y())
        self.contextMenu.exec_(QCursor.pos())  # 在鼠标位置显示

    def downLoadAct(self):
        t = Thread(target=self.downLoad)
        t.start()

    def downLoad(self):
        try:
            bv = self.mList[self.selectIndex]['bv_id']
            audioUrl = self.spider.audioUrl('https://www.bilibili.com/video/' + bv)
            print(audioUrl)
            self.spider.download(self.mList[self.selectIndex]['title'], audioUrl)
        except Exception as e:
            raise e


if __name__ == '__main__':
    # 创建应用程序和对象
    app = QApplication(sys.argv)
    mp = MusicList()
    sys.exit(app.exec_())
